import {Component, ElementRef, Input, OnInit} from '@angular/core';
import {ModalController} from "@ionic/angular";
import {Router} from "@angular/router";
import {API} from "../../services/API";
import {HttpService} from "../../services/http/http.service";
import {MapLocationService} from "../../services/map-location.service";
declare var  AMap: any;

@Component({
  selector: 'app-address-linkage',
  templateUrl: './address-linkage.component.html',
  styleUrls: ['./address-linkage.component.scss'],
})
export class AddressLinkageComponent implements OnInit {
  flag = true;
  provincial = '';
  areas;
  pinYin;
  province; // 省
  list = [];
  cityList = [];
  area = '正在定位中...';
  adcode;
  cityCode = '';
  sourceLatitude = '';
  sourceLongitude = '';
  constructor(
      private modalController: ModalController,
      private router: Router,
      private httpService: HttpService,
      private ref: ElementRef,
      private mapLocation: MapLocationService,
  ) { }

  async ngOnInit() {
    this.getHotCity();
    this.getLocation();
    const info = await this.httpService.post(API.COMMUNITY_PROVINCE, {}, false);
    console.log(info);
    if (info) {
      this.list = [];
      this.areas = Object.values(info);
      this.pinYin = Object.keys(info);
      console.log(this.areas);
      for (let i = 0; i< this.areas.length; i++){
        this.list.push({
          areas:this.areas[i],
          pinYin: this.pinYin[i]
        })
      }
    }
  }

  async getHotCity(){
    const info = await this.httpService.post(API.COMMUNITY_GET_HOST_CITY, {}, false);
    console.log(info);
    if (info){
      if (info.key){
        this.cityList = info.key;
        for (let item of this.cityList){
          item.isSelect = false;
        }
      }
    }
  }
  async onPageDidEnter() {

  }
  async selectArea(items) {
    if (items.level == 1) {
      this.provincial = items.area;
      console.log('打印地理信息');
      console.log(items);
      this.province = items.name;
      console.log(this.province);
      const param = {
        id: items.id
      }
      const info = await this.httpService.post(API.COMMUNITY_CITY, param, false);
      console.log(info);
      if (info) {
        this.list = [];
        this.areas = Object.values(info);
        this.pinYin = Object.keys(info);
        console.log(this.areas);
        if (this.areas.length < 1 ) {
          const data = {
            code: items.code,
            option: items.name,
            province: this.province,
          }
            this.modalController.dismiss(data);
        } else {
          for (let i = 0; i< this.areas.length; i++){
            this.list.push({
              areas:this.areas[i],
              pinYin: this.pinYin[i]
            })
          }
        }
        console.log(this.list);
      }
    } else {
      const data = {
        code: items.code,
        option: items.name,
        province: this.province,
      }
      console.log(data);
      this.modalController.dismiss(data);
    }
  }

  close() {
    if(this.area && this.cityCode){
      const data = {
        code: this.cityCode,
        option: this.area,
        isClose: 'close',
      }
      this.modalController.dismiss(data);
    } else {
      this.modalController.dismiss();
    }

  }

  // 选择该item
  onSelect(item,i) {
    console.log(111);
    console.log(item);
    item.isSelect = !item.isSelect;
    for (let j = 0; j < this.cityList.length; j++) {
      if (i === j) {
        item.isSelect = true;
      } else {
        this.cityList[j].isSelect = false;
      }
    }
    if(item.itemValue){
      const data = {
        code: item.itemInfo,
        option: item.itemValue,
        province: this.province,
      }
      console.log(data);
      this.modalController.dismiss(data);
    }
  }

  async getLocation() {
    if (this.area == '正在定位中...') {
      const mapInfo = await this.mapLocation.getUserLocationAsync().catch(() => {
        console.log('获取定位失败');
      });
      console.log(JSON.stringify(mapInfo));
      if (mapInfo) {
        this.sourceLatitude = mapInfo.latitude;
        this.sourceLongitude = mapInfo.longitude;
        const param = {
          sourceLatitude: this.sourceLatitude,
          sourceLongitude: this.sourceLongitude,
        }
        const info = await this.httpService.post(API.BANK_ACCOUNT_GET_CITY_CODE, param, false);
        console.log(112, info);
        if (info) {
          this.area = info.name;
          this.cityCode = info.code;
        }
      }
    } else if (this.area &&  this.cityCode){
      const data = {
        code: this.cityCode,
        option: this.area,
      }
      this.modalController.dismiss(data);
    }
  }
}
